﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace AsteroidWar
{
    class MenuManager
    {
        public enum MenuStates { Main, Multi, Difficult, Demo, Exit, None , Model};
        public MenuStates menuStates = MenuStates.Main;
        public MenuStates previousState = MenuStates.None;

        List<Button> buttonList = new List<Button>();
        ContentManager contentManager;

        public bool isPlaying;
        public bool isStop;
        public bool isDemo;
        MouseState previousMouseState;
        MouseState currentMouseState;
        public MenuManager(ContentManager contentManager)
        {
            this.isPlaying = false;
            this.isStop = false;
            this.contentManager = contentManager;
        }

        public void InitMainmenu()
        {
            Texture2D startGameButton = contentManager.Load<Texture2D>(@"Textures\Buttons\startgame");
            Texture2D demoButton = contentManager.Load<Texture2D>(@"Textures\Buttons\demo");
            //Texture2D difficultyButton = contentManager.Load<Texture2D>(@"Textures\Buttons\difficulty");
            Texture2D exitButton = contentManager.Load<Texture2D>(@"Textures\Buttons\exit");

            Button startGame = new Button("Start game", startGameButton, new Vector2(250, 350));
            Button demo = new Button("Demo", demoButton, new Vector2(250, 425));
            //Button difficulty = new Button("Difficulty", difficultyButton, new Vector2(250, 425));
            Button exit = new Button("Exit", exitButton, new Vector2(250, 500));

            buttonList.Clear();
            buttonList.Add(startGame);
            buttonList.Add(demo);
            //buttonList.Add(difficulty);
            buttonList.Add(exit);
        }

        public void InitMultimenu()
        {
            Texture2D _1PlayerButton = contentManager.Load<Texture2D>(@"Textures\Buttons\1player");
            Texture2D _2PlayersButton = contentManager.Load<Texture2D>(@"Textures\Buttons\2players");
            Texture2D backButton = contentManager.Load<Texture2D>(@"Textures\Buttons\back");


            Button _1Player = new Button("1 Player", _1PlayerButton, new Vector2(250, 350));
            Button _2Players = new Button("2 Players", _2PlayersButton, new Vector2(250, 425));
            Button back = new Button("Back", backButton, new Vector2(250, 500));

            buttonList.Clear();
            buttonList.Add(_1Player);
            buttonList.Add(_2Players);
            buttonList.Add(back);
        }

        public void InitDifficult()
        {
            Texture2D normalButton = contentManager.Load<Texture2D>(@"Textures\Buttons\normal");
            Texture2D hardButton = contentManager.Load<Texture2D>(@"Textures\Buttons\hard");
            Texture2D backButton = contentManager.Load<Texture2D>(@"Textures\Buttons\back");


            Button normal = new Button("Normal", normalButton, new Vector2(250, 350));
            Button hard = new Button("Hard", hardButton, new Vector2(250, 425));
            Button back = new Button("Back", backButton, new Vector2(250, 500));

            buttonList.Clear();
            buttonList.Add(normal);
            buttonList.Add(hard);
            buttonList.Add(back);
        }

        public void InitModel()
        {
            Texture2D model1 = contentManager.Load<Texture2D>(@"Textures\Buttons\fighter-Archimedes1");
            Texture2D model2 = contentManager.Load<Texture2D>(@"Textures\Buttons\Fighter-Viper Mk");
            Texture2D model3 = contentManager.Load<Texture2D>(@"Textures\Buttons\Shuttle-Raptor2");
            Texture2D backButton = contentManager.Load<Texture2D>(@"Textures\Buttons\back");

            Button model1Button = new Button("Model 1", model1, new Vector2(91, 230));
            Button model2Button = new Button("Model 2", model2, new Vector2(401, 230));
            Button model3Button = new Button("Model 3", model3, new Vector2(246, 360));
            Button back = new Button("Back", backButton, new Vector2(250, 500));

            model1Button.setSize(300, 131);
            model2Button.setSize(300, 131);
            model3Button.setSize(300, 131);

            buttonList.Clear();
            buttonList.Add(model1Button);
            buttonList.Add(model2Button);
            buttonList.Add(model3Button);
            buttonList.Add(back);
        }

        protected void ChangeMainState(MouseState mouseState)
        {
            if (checkMouseState(mouseState))
            {
                foreach (Button button in buttonList)
                {
                    button.Update(mouseState);
                    if (button.isClicked)
                    {
                        switch (button.name)
                        {
                            case "Start game":
                                menuStates = MenuStates.Multi;
                                break;
                            case "Demo":
                                previousState = MenuStates.Demo;
                                isDemo = true;
                                break;
                            case "Difficulty":
                                menuStates = MenuStates.Difficult;
                                break;
                            case "Exit":
                                menuStates = MenuStates.Exit;
                                isStop = true;
                                break;
                        }
                        break;
                    }
                }
            }
        }

        protected void ChangeMultiState(MouseState mouseState)
        {
            if (checkMouseState(mouseState))
            {
                foreach (Button button in buttonList)
                {
                    button.Update(mouseState);
                    if (button.isClicked)
                    {
                        switch (button.name)
                        {
                            case "1 Player":
                                GameSettings._1player = true;
                                GameSettings._2players = false;
                                menuStates = MenuStates.Difficult;
                                previousState = MenuStates.Multi;
                                break;
                            case "2 Players":
                                GameSettings._1player = false;
                                GameSettings._2players = true;
                                menuStates = MenuStates.Difficult;
                                previousState = MenuStates.Multi;
                                break;
                            case "Back":
                                menuStates = MenuStates.Main;
                                break;
                        }
                        break;
                    }
                }
            }
        }

        protected void ChangeDifficultState(MouseState mouseState)
        {
            if (checkMouseState(mouseState))
            {
                foreach (Button button in buttonList)
                {
                    button.Update(mouseState);
                    if (button.isClicked)
                    {
                        switch (button.name)
                        {
                            case "Normal":
                                GameSettings._normal = true;
                                GameSettings._hard = false;
                                menuStates = MenuStates.Model;
                                break;
                            case "Hard":
                                GameSettings._normal = false;
                                GameSettings._hard = true;
                                menuStates = MenuStates.Model;
                                break;
                            case "Back":
                                menuStates = MenuStates.Multi;
                                break;
                        }
                        break;
                    }
                }
            }
        }

        protected void ChangeModelState(MouseState mouseState)
        {
            if (checkMouseState(mouseState))
            {
                foreach (Button button in buttonList)
                {
                    button.Update(mouseState);
                    if (button.isClicked)
                    {
                        switch (button.name)
                        {
                            case "Model 1":
                                GameSettings._model = 1;
                                isPlaying = true;
                                previousState = MenuStates.Model;
                                break;
                            case "Model 2":
                                GameSettings._model = 2;
                                isPlaying = true;
                                previousState = MenuStates.Model;
                                break;
                            case "Model 3" :
                                GameSettings._model = 3;
                                isPlaying = true;
                                previousState = MenuStates.Model;
                                break;
                            case "Back":
                                menuStates = MenuStates.Difficult;
                                break;
                        }
                        break;
                    }
                }
            }
        }

        public void Update(GameTime gametime, MouseState mousetate)
        {
            switch (menuStates)
            {
                case MenuStates.Main:
                    if (previousState == MenuStates.None || previousState != menuStates)
                    {
                        previousState = menuStates;
                        InitMainmenu();                  
                    }
                    ChangeMainState(mousetate);
                    break;
                case MenuStates.Multi:
                    if (previousState != MenuStates.Multi)
                    {
                        previousState = menuStates;
                        InitMultimenu();
                    }
                    ChangeMultiState(mousetate);
                    break;
                case MenuStates.Difficult:
                    if (previousState != MenuStates.Difficult)
                    {
                        previousState = menuStates;
                        InitDifficult();
                    }
                    ChangeDifficultState(mousetate);
                    break;
                case MenuStates.Model:
                    if (previousState != MenuStates.Model)
                    {
                        previousState = menuStates;
                        InitModel();
                    }
                    ChangeModelState(mousetate);
                    break;
            }

           
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Button button in buttonList)
            {
                button.Draw(spriteBatch);
            }
        }

        private bool checkMouseState(MouseState mouseState)
        {
            currentMouseState = mouseState;
            bool result = true;
            if (currentMouseState.LeftButton == ButtonState.Pressed && currentMouseState != previousMouseState)
            {
                //Allow change state
                result = true;
            }
            if (currentMouseState.LeftButton == ButtonState.Pressed && currentMouseState == previousMouseState)
            {
                //Deny change state
                result = false;
            }
            previousMouseState = currentMouseState;
            return result;
        }

       
    }

}
